import { reqAddUser } from "../../api";

let userAddForm = document.getElementById('userAddForm');
let username = document.getElementById('username');
let pwd = document.getElementById('pwd');
let profile = document.getElementById('profile');
let prevImg = document.getElementById('prevImg');

//提交新增用户
userAddForm.onsubmit =async function(e){
    //取消浏览器默认行为
    e.preventDefault();

    //添加表单判断 - 内容是否为空

    //获取到表单数据
    let formData = new FormData(userAddForm);

    console.log(formData);

   let result =  await reqAddUser(formData);

//    console.log(result);
    if(result.code === 2000){
        window.location.href = '/users.html';
    }else{
        alert(result.message);
    }

}

//预览头像功能
profile.onchange = function(e){
    //获取到你选择的图片信息
    let readFile = new FileReader();

    //图片文件
    let img = e.target.files[0];

    //使用DataURL方式读取img
    readFile.readAsDataURL(img);

    //当文件读取完成后
    readFile.onload = function(){
        let proFileImg = readFile.result;

        //将读取到的图片显示在预览img标签上
        prevImg.src = proFileImg;
    }
}

